/**
 * Copyright (C) 2009, 2010 SC 4ViewSoft SRL
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mDigitalHealth.chart;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import org.achartengine.tools.ZoomEvent;
import org.achartengine.tools.ZoomListener;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.os.Bundle;
import android.view.WindowManager.LayoutParams;
import android.webkit.WebView;
import android.widget.LinearLayout;

import com.mDigitalHealth.R;
import com.mDigitalHealth.common.Utils;
import com.mDigitalHealth.model.LineChartInfo;
import com.mDigitalHealth.network.DigitalHealthClient;

public class ChartStepEasy extends Activity {

	// view
	// private GraphicalView chart;

	// 渲染器
	// private XYMultipleSeriesRenderer renderer;

	// 格式
	private final static String FROMAT = "MM-dd";

	// private XYMultipleSeriesDataset dataset;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chart_step_easy);

		GraphicalView chart1 = getSetpGraphicalView();

		LinearLayout barchart = (LinearLayout) findViewById(R.id.chartstep);

		barchart.addView(chart1, new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT));
		WebView webView = new WebView(this);

		barchart.addView(webView, new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT));
		// chart.addZoomListener(new zoomChange(), true, true);
		// chart.get

	}

	private GraphicalView getSetpGraphicalView() {
		String[] titles = new String[] { "每日步数" };

		LineChartInfo lineChart = DigitalHealthClient.getDailyStep(Utils.getUsername(this));
		// setting X
		long[] x = lineChart.getX();

		// setting Y
		double[] y = lineChart.getY();

		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		dataset = buildDataset(dataset, titles, x, y);

		int[] colors = new int[] { Color.BLUE };
		PointStyle[] styles = new PointStyle[] { PointStyle.POINT };

		XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles, true);

		setChartSettings(renderer, "步数趋势图", "日", "步数(千)", lineChart.getMinX(), lineChart.getMaxX()
				+ lineChart.getMaxX() * 0.00001, lineChart.getMinY() - 10,
				lineChart.getMaxY() + 20, Color.LTGRAY, Color.LTGRAY);
		renderer.setXLabels(9);
		renderer.setYLabels(10);
		renderer.setShowGrid(true);
		renderer.setXLabelsAlign(Align.CENTER);
		renderer.setYLabelsAlign(Align.RIGHT);

		// 设置背景色
		renderer.setApplyBackgroundColor(true);
		renderer.setBackgroundColor(Color.WHITE);
		// 表格外的边框色
		renderer.setMarginsColor(Color.WHITE);
		renderer.setGridColor(Color.BLACK);
		// 坐标值颜色
		renderer.setLabelsColor(Color.BLACK);
		// 坐标轴颜色
		renderer.setAxesColor(Color.BLACK);

		// renderer.setMargins(new int[]{20,20,20,60});
		// renderer.set
		int length = renderer.getSeriesRendererCount();

		for (int i = 0; i < length; i++) {
			SimpleSeriesRenderer ssr = renderer.getSeriesRendererAt(i);
			// 不知道作者的居中是怎么计算的,默认是Align.CENTER,但是对于两个以上的条形显示
			// 就画在了最右边
			ssr.setChartValuesTextAlign(Align.RIGHT);
			// 点的数字字体大小
			ssr.setChartValuesTextSize(12);
			// 显示点数值
			ssr.setDisplayChartValues(true);
		}
		// renderer.getSeriesRendererAt(0).setDisplayChartValues(true);
		renderer.setZoomButtonsVisible(true);
		// 以下两个限制加上后x轴不可调整
		// renderer.setPanLimits(new double[] { -10, 2000, -10, 2000 });
		// renderer.setZoomLimits(new double[] { -10, 2000, -10, 2000 });

		GraphicalView chart = ChartFactory.getTimeChartView(this, dataset, renderer, FROMAT);

		return chart;
	}

	protected XYMultipleSeriesDataset buildDataset(XYMultipleSeriesDataset dataset,
			String[] titles, long[] xValues, double[] yValues) {

		if (dataset == null) {
			dataset = new XYMultipleSeriesDataset();
		}

		int length = titles.length; // 有几条线
		XYSeries series = new XYSeries(titles[0]); // 根据每条线的名称创建

		int seriesLength = xValues.length; // 有几个点

		for (int k = 0; k < seriesLength; k++) // 每条线里有几个点
		{
			series.add(xValues[k], yValues[k]);
		}

		dataset.addSeries(series);

		return dataset;
	}

	protected XYMultipleSeriesRenderer buildRenderer(int[] colors, PointStyle[] styles, boolean fill) {
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
		int length = colors.length;
		for (int i = 0; i < length; i++) {
			XYSeriesRenderer r = new XYSeriesRenderer();
			r.setColor(colors[i]);
			r.setPointStyle(styles[i]);
			r.setFillPoints(fill);
			// 线的颜色
			// r.setColor(Color.YELLOW);
			// r.setFillBelowLineColor(Color.BLACK);
			// 设置线宽度
			r.setLineWidth(6.0f);
			renderer.addSeriesRenderer(r);

			// 如果有2个以上的图形是相邻的设为不同的背景色
			if (i % 2 == 0) {
				// 设置页边空白的颜色
				// renderer.setMarginsColor(Color.CYAN);
			}
		}
		return renderer;
	}

	protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle,
			String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor,
			int labelsColor) {
		renderer.setChartTitle(title);
		renderer.setXTitle(xTitle);
		renderer.setYTitle(yTitle);
		renderer.setXAxisMin(xMin);
		renderer.setXAxisMax(xMax);
		renderer.setYAxisMin(yMin);
		renderer.setYAxisMax(yMax);
		renderer.setAxesColor(axesColor);
		renderer.setLabelsColor(labelsColor);
	}

	/*
	 * 缩放监听处理
	 */
	private class zoomChange implements ZoomListener {
		@Override
		public void zoomApplied(ZoomEvent arg0) {
			// TODO Auto-generated method stub
			if (arg0.isZoomIn()) {
				// chart.
				// dataset = null;
			}
		}

		@Override
		public void zoomReset() {
			// TODO Auto-generated method stub

		}
	}

}